package yun.http.rechochoku.response;

import org.json.JSONException;
import org.json.JSONObject;
import yun.common.http.request.Request;
import yun.common.http.response.ErrorResponse;
import yun.common.http.response.IResponse;
import yun.common.http.response.JSONResponseFactory;
import yun.http.rechochoku.request.Au01Request;
import android.util.Log;

public class BaseResponseFactory extends JSONResponseFactory {
	private static final String TAG = "ResponseFactory";
	@Override
	protected IResponse newJSONResponse(Request request, String jsonString) {

		Log.d(TAG, "newJSONResponse()" + jsonString);

		// init
		JSONObject json = null;
		try {
			json = new JSONObject(jsonString);
		} catch (JSONException e) {
			return new ErrorResponse(ErrorResponse.UNKNOWN, e.getMessage());
		}

		// failure
		try {
			return new FailureResponse().parse(json);
		} catch (Exception e) {
			// ignore
		}

		// success
		try {
			if (request instanceof Au01Request) {
				return new Au01Response().parse(json);
			} else if (request instanceof Au01Request) {
				return new Au01Response().parse(json);
			}
		} catch (JSONException e) {
			return new ErrorResponse(ErrorResponse.UNKNOWN, e.getMessage());
		}

		// unmatched url
		return new ErrorResponse(ErrorResponse.NOT_FOUND_RESPONSE);
	}
}
